package com.gpy.hospital.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gpy.hospital.cms.dao.UserMapper;
import com.gpy.hospital.cms.pojo.User;
import com.gpy.hospital.cms.service.IUserService;
import com.gpy.hospital.common.Const;
import com.gpy.hospital.common.ServerResponse;
import com.gpy.hospital.util.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author
 * @Date 2020/2/26 21:59
 * @Desc
 **/
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<User> getByEmail(String userEmail, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.getByUserEmail(userEmail);
        return new PageInfo<>(users);
    }

    @Override
    public User getById(Integer userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    /**
     * 更新用户信息
     *
     * @param user
     * @return
     */
    @Override
    public ServerResponse updateUser(User user) {
        int result = 0;
        if (StringUtils.isBlank(user.getPassword())) {
            // 不更新密码
            result = userMapper.updateByPrimaryKeyNoPassword(user);
        } else {
            user.setPassword(MD5Util.getSaltMD5(user.getPassword()));
            // 更新密码
            result = userMapper.updateByPrimaryKey(user);
        }
        if (result > 0) {
            return ServerResponse.createByErrorMessage("更新成功！");
        }
        return ServerResponse.createByErrorMessage("更新失败！");
    }

    @Override
    public ServerResponse deleteById(Integer userId) {
        int result = userMapper.deleteByPrimaryKey(userId);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("删除成功！");
        }
        return ServerResponse.createByErrorMessage("删除失败！");
    }

    @Override
    public ServerResponse login(HttpServletRequest request, User user) {
        List<User> byUserEmail = userMapper.getByUserEmail(user.getEmail());
        if (byUserEmail == null || byUserEmail.size() == 0) {
            return ServerResponse.createByErrorMessage("账户号不存在！");
        }
        if (!MD5Util.getSaltVerifyMD5(user.getPassword(), byUserEmail.get(0).getPassword())) {
            return ServerResponse.createByErrorMessage("请检查密码！");
        }
        request.getSession().setAttribute(Const.CURRENT_USER, byUserEmail.get(0));
        return ServerResponse.createBySuccessMessage("登录成功！");
    }

    @Override
    public ServerResponse register(User user) {
        List<User> byUserEmail = userMapper.getByUserEmail(user.getEmail());
        if (byUserEmail != null || byUserEmail.size() != 0) {
            return ServerResponse.createByErrorMessage("账户号已存在！");
        }
        user.setPassword(MD5Util.getSaltMD5(user.getPassword()));
        user.setStatus(1);
        user.setRole(1);
        int result = userMapper.insert(user);
        if (result > 0) {
            return ServerResponse.createByErrorMessage("注册成功！");
        }
        return ServerResponse.createByErrorMessage("注册失败！");
    }

    @Override
    public ServerResponse adminLogin(HttpServletRequest request,User user) {
        List<User> byUserEmail = userMapper.getByUserEmail(user.getEmail());
        if (byUserEmail == null || byUserEmail.size() == 0) {
            return ServerResponse.createByErrorMessage("账户号不存在！");
        }
        if (!MD5Util.getSaltVerifyMD5(user.getPassword(), byUserEmail.get(0).getPassword())) {
            return ServerResponse.createByErrorMessage("请检查密码！");
        }
        request.getSession().setAttribute(Const.CURRENT_USER, byUserEmail.get(0));
        return ServerResponse.createBySuccess("登录成功！", byUserEmail.get(0));
    }

    @Override
    public ServerResponse addUser(User user) {
        user.setRole(3);
        user.setPassword(MD5Util.getSaltMD5(user.getPassword()));
        int result = userMapper.insert(user);
        if (result > 0) {
            return ServerResponse.createByErrorMessage("成功！");
        }
        return ServerResponse.createByErrorMessage("失败！");
    }
}
